<?php

namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\PaymentLog;
use app\common\model\User;
use app\common\model\UserLike;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['rank','dynamicList','getPayInfo'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }


    /**
     *排行榜
     */
    public function rank()
    {
        $types = $this->request->post('types');
        $isFamily = $this->request->post('is_family');
        $where = [];
        if(!empty($isFamily)){
            if($this->auth->id){
                $where['surname'] = $this->auth->surname;
            }else{
                $this->error(__('Please login first'), null, 401);
            }
        }
        $list = [];
        if($types == 1){
            $list = User::where($where)->order('score','desc')->field('id,nickname,avatar,money,score,like_num,education')->select();
        }
        if($types == 2){
            $list = User::where($where)->where('birthday IS NOT NULL')->order('birthday','asc')->field('id,nickname,avatar,birthday,like_num,education')->select();
            foreach($list as $v){
                $v->age = birthday($v->birthday) . '岁';
            }
        }
        if($types == 3){
            $list = User::where($where)->order('reward','desc')->field('id,nickname,avatar,reward,like_num,education')->select();
        }
        if($types == 4){
            $list = User::where($where)->order('education','desc')->field('id,nickname,avatar,education,like_num,education')->select();
        }
        if(!empty($list)){
            $list = $list->toArray();
        }
        foreach($list as $k=>$v){
            $list[$k]['is_like '] = UserLike::where('user_id',$this->auth->id)->where('to_user_id',$v['id'])->whereTime('created_at','today')->count();
            if($types != 4){
                unset($list[$k]['education_name']);
            }
        }
        $this->success('请求成功',$list);
    }


    /**
     * 用户点赞
     */
    public function like(){
        $userId = request()->post('user_id');
        $count = User::where('id',$userId)->count();
        if($count < 1){
            $this->error('参数错误');
        }
        $result = UserLike::addLike($this->auth->id,$userId);
        $this->success($result['msg'],[],$result['code']);
    }

    public function getPayInfo(){
        $money = request()->post('money');
        $payMethod = request()->post('pay_method',1); // 1=支付宝,2=微信
        if(empty($money) || $money <= 0){
            $this->error('参数错误');
        }
        if(!in_array($payMethod,[1,2])){
            $this->error('参数错误');
        }

        if($money < 1){
            $this->error('充值金额不能小于1');
        }
        //$money = 0.01;
        if(request()->post('shop_order_id')){
            $a =  Db::connect('db_config2')->name('ewei_shop_order')->where('id',request()->post('shop_order_id'))->find();
            if(empty($a)){
                $this->error('订单不存在');
            }else{
                $orderNo = $a['ordersn'];
            }
        }else{
            $orderNo = generateUniqid('ZF');
        }
        //$order = [
        //    'out_trade_no' => $orderNo,
        //    'total_fee' => $money, // **单位：分**
        //    'body' => '充值',
        //];
        //
        //if($payMethod == 2){
        //    $pay = new WechatPay();
        //}else{
        //    $pay = new Alipay();
        //}
        //$result = $pay->send($order,'app');
        if($this->auth->id){
            PaymentLog::create([
                'pay_no'    => $orderNo,
                'user_id'   => $this->auth->id,
                'money'     => $money,
                'method'    => $payMethod,
            ]);
        }
        return $this->success('请求成功',[
            'channel'=>$payMethod == 2 ? 0 : 1,
            'orderid'=> $orderNo,
            'title'  => '充值',
            'amount' => $money
        ]);
    }
}
